<template>
  <div class="container">
    <div class="top">中央动态</div>
    <div class="box">
      <div class="top1">
        <div class="left">
          <span>标题：</span>
          <el-input v-model="input1" class="first"></el-input>
          <span>是否上架：</span>
          <el-select v-model="value" placeholder="请选择" class="second">
            <el-option
            v-show="_SHOWBTN('wzgl-xsxc-zydt-sj')"
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
          <!-- <span>创建/更新日期：</span>
          <el-date-picker v-model="value2" type="date" placeholder="选择日期" class="third"></el-date-picker>-->
          <el-button type="primary" @click="search">查询</el-button>
          <el-button @click="reset">重置</el-button>
        </div>
        <div class="right">
          <el-button type="primary" @click="add" class="add" v-if="_SHOWBTN('wzgl-xsxc-zydt-xj')">+新建</el-button>
          <!-- <ReportBuild :get="get"></ReportBuild> -->
        </div>
      </div>
      <!-- 表格 -->
      <el-table
        :data="tableList"
        style="width:1540px"
        border
        stripe
        :cell-style="changeCellStyle"
        :header-cell-style="rowClass"
      >
        <el-table-column type="index" label="序号" width="52px" align="center"></el-table-column>
        <el-table-column prop="title" label="标题"></el-table-column>

        <el-table-column prop="status" label="是否上架" align="center" width="140px">
          <template slot-scope="scope">
            <el-switch
            
              v-model="scope.row.isTrue"
              active-color="#1677FF"
              inactive-color="#E8E8E8"
              @change="userStateChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" align="center" width="140px">
          <template slot-scope="scope">
            <span class="up" @click="up(scope.row.id)">
              <img src="@/assets/image/up.png" alt />
            </span>
            <span class="down" @click="down(scope.row.id)">
              <img src="@/assets/image/down.png" alt />
            </span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="223px">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="edit(scope.row.id)" class="five" v-if="_SHOWBTN('wzgl-xsxc-zydt-xg')">编辑</el-button>
            <!-- 编辑 -->
            <!-- <ReportEdit :id="scope.row.id" :get="get"></ReportEdit> -->
            <!-- 删除 -->
            <SingleDeletion :clearOne="clearOne" :id="scope.row.id" showBetCode="wzgl-xsxc-zydt-sc"></SingleDeletion>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="fenye">
        <Pagination :total="total" :params=" params" :get="get"></Pagination>
      </div>
    </div>
  </div>
</template>
<script>
import SingleDeletion from "@/components/Popup/SingleDeletion.vue";
import Pagination from "@/components/Popup/Pagination.vue";
// import ReportBuild from "@/components/Center/ReportBuild.vue";
// import ReportEdit from "@/components/Center/ReportEdit.vue";
export default {
  components: {
    SingleDeletion,
    // ReportBuild,
    // ReportEdit,
     Pagination,
  },
  data() {
    return {
      params: {
        pageNum: 1,
        publishStatus: "",
        title: "",
        pageSize: 10,
      },
      id: 0,
      // 表格列表
      tableList: [],
      // 时间选择
      value1: "",
      // 总条数
      // 总条数
      total: 0,
      page: 1,
      size: 10,
      // 标题
      input1: "",
      visible: true,
      imageUrl: "",

      input1: "",
      input2: "",
      value: "",
      value2: "",

      options: [
        {
          value: "",
          label: "全部",
        },
        {
          value: 5,
          label: "已上架",
        },
        {
          value: 1,
          label: "未上架",
        },
      ],
      ruleForm: {
        name: "",
        classify: "",
        link: "",
      },
      rules: {
        name: [{ required: true, message: "请输入标题", trigger: "blur" }],
        classify: [{ required: true, message: "请输入分类", trigger: "blur" }],
        link: [{ required: true, message: "请输入链接", trigger: "blur" }],
      },
    };
  },
  created() {
    this.get();
  },
  methods: {
    add() {
      this.$router.push({name:'ZH'});
    },
    // 编辑
    edit(id) {
      this.$router.push({ name: "ZHBJ", query: { id: id } });
    },
    // 获取列表
    get() {
      this.Api.getCenter(this.params).then((res) => {
        if (res.code == 0) {
          this.tableList = res.data.records;

          this.tableList.map((item) => {
            if (item.publishStatus == 5) {
              this.$set(item, "isTrue", true);
            } else if (item.publishStatus == 1) {
              this.$set(item, "isTrue", false);
            }
          });

          this.total = res.data.total;
        }
      });
    },

    // // 分页
    // //数据条数发生变化
    // handleSizeChange(newSize) {
    //   this.size = newSize;
    //   if (this.value == "" && this.input1 == "") {
    //     this.get();
    //   } else {
    //     this.search();
    //   }
    // },
    // //翻页触发
    // handleCurrentChange(currPage) {
    //   this.page = currPage;

    //   if (this.value == "" && this.input1 == "") {
    //     this.get();
    //   } else {
    //     this.search();
    //   }
    // },
    // 是否上架状态改变
    userStateChange(row) {
      // 上架
      if (row.isTrue == true) {
        this.Api.lookUp({ id: row.id }).then((res) => {
          if (res.code == 0) {
            return this.$message.success("上架成功");
          } else {
            row.isTrue = !row.isTrue;
            return this.$message.error("上架失败");
          }
        });
      }
      // 下架
      if (row.isTrue == false) {
        this.Api.lookDown({ id: row.id }).then((res) => {
          if (res.code == 0) {
            return this.$message.success("下架成功");
          } else {
            row.isTrue = !row.isTrue;
            return this.$message.error("下架失败");
          }
        });
      }
    },
    // 搜索
    search() {
      var dateTime = this.value1;
      // 开始时间
      var startDate = "";
      // 结束时间
      var endDate = "";
      if (dateTime != null && dateTime != "" && dateTime != undefined) {
        startDate = this.value1[0];
        endDate = this.value1[1];
      }
      this.Api.getCenter({
        pageNum: this.page,
        pageSize: this.size,
        publishStatus: this.value,
        title: this.input1,
      }).then((res) => {
        console.log(res);
        this.tableList = res.data.records;
        this.tableList.map((item) => {
          if (item.publishStatus == 5) {
            this.$set(item, "isTrue", true);
          } else if (item.publishStatus == 1) {
            this.$set(item, "isTrue", false);
          }
        });
        this.total = res.data.total;
      });
    },
    // 重置
    reset() {
      (this.value = ""), (this.input1 = ""),
      this.params={};
      this.params={
          pageNum: 1,
        publishStatus: "",
        title: "",
        pageSize: 10,
      }
      this.get();
    },
    changeCellStyle(row, column, rowIndex, columnIndex) {
      if (row.column.label === "标题") {
        return "padding-left: 20px"; // 修改的样式
      } else {
        return "";
      }
    },
    rowClass({ row, column, rowIndex, columnIndex }) {
      if (rowIndex === 0 && columnIndex === 1) {
        //指定坐标
        return "padding-left:12px";
      } else {
        return "";
      }
    },
    // 删除
    clearOne(id) {
      let ID = { id: id };
      this.Api.centerDelete(ID).then((res) => {
        if (res.code == 0) {
          this.$message.success("删除成功");
          this.get();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    // 上移
    up(id) {
      let ID = { id: id };
      this.Api.lookMove1(ID).then((res) => {
        console.log(res);
        this.get();
      });
    },
    // 下移
    down(id) {
      let ID = { id: id };
      this.Api.lookMove(ID).then((res) => {
        this.get();
      });
    },
  },
};
</script>
<style lang="less" scoped>
.top {
  width: 1540px;
  height: 68px;
  line-height: 68px;
  color: #333333;
  font-size: 20px;
  border-bottom: 1px solid #dddddd;
  box-sizing: border-box;
}

.top1 {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 35px;
}
.left {
  display: flex;
  margin-top: 19px;
  align-items: center;
}
.right {
  margin-top: 19px;
}
/deep/.left .el-input__inner {
  width: 154px;
  height: 36px;
}
/deep/.left .el-input {
  width: 154px;
}
.left span {
  height: 36px;
  line-height: 36px;
  font-size: 14px;
  color: #333333;
}
.el-button {
  width: 52px;
  height: 36px;
  padding: 0;
}
.five {
  width: 60px;
  height: 28px;
  margin-right: 21px;
}
.first,
.second,
.third {
  margin-right: 20px;
}
/deep/ .add.el-button {
  width: 80px;
}
/deep/.el-pagination {
  white-space: nowrap;
  padding: 2px 5px;
  color: #303133;
  font-weight: 700;
  margin-left: 850px;
  margin-top: 46px;
}
/deep/.el-pagination__jump {
  margin-left: 0;
  font-weight: 400;
  color: #606266;
}
.up {
  margin-right: 23px;
}
/deep/.el-dialog__header {
  padding: 20px 20px 10px;
  background: #e6e9ed;
}

/deep/.el-form-item__label {
  width: 82px !important;
  text-align: right;
  vertical-align: middle;
  float: left;
  font-size: 14px;
  color: #606266;
  line-height: 40px;
  padding: 0 12px 0 0;
  box-sizing: border-box;
}

/deep/tbody {
  tr {
    height: 45px !important;
    td {
      padding: 0px 0 !important;
    }
  }
  tr td:last-child {
    .cell {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  }
}
/deep/tbody {
  tr td:nth-child(2) {
    .cell {
      padding-left: 20px;
    }
  }
}
.fenye {
  display: flex;
  justify-content: flex-end;
}
</style>